package com.example.interceptor_demo.config;

import com.example.interceptor_demo.common.AppVar;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义拦截器
 */
@Component
public class UserInterceptor implements HandlerInterceptor {

    /**
     * 返回 true ->  拦截器验证成功,继续执行后续的方法
     *     false -> 拦截器验证失败,不会执行后续方法
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {


        System.out.println("do UserInterceptor");
        //业务方法
        HttpSession session = request.getSession(false); //这里默认是true,
        if (session != null && session.getAttribute(AppVar.SESSION_KEY) != null) {
            // 用户已经登录
            return true;
        }
        //response.sendRedirect("https://www.baidu.com");
        return false;
    }
}
